/**
 * Ingenius-Group S.A.S
 *
 * Project: SAU
 * File: EditIndicator2EvaluatePresenter.java
 * Date: 07/02/2011
 */

package com.ingenius.sau.client.presenter;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HasWidgets;
import com.ingenius.sau.client.AppController;
import com.ingenius.sau.client.Constants;
import com.ingenius.sau.client.event.ListIndicators2EvaluateEvent;
import com.ingenius.sau.client.service.CoreServiceAsync;
import com.ingenius.sau.client.view.Display;
import com.ingenius.sau.shared.model.core.Feature2Evaluate;
import com.ingenius.sau.shared.model.core.Indicator2Evaluate;

/**
 * Class <strong>EditIndicator2EvaluatePresenter</strong>
 * 
 * @author Jesus David
 * @version 0.1
 */
public class EditIndicator2EvaluatePresenter implements EditPresenter {

	public interface EditIndicator2EvaluateDisplay extends Display {

		String getName();

		void setName(String name);

		String getDescription();

		void setDescription(String description);

		float getWeighing();

		void setWeighing(float weighing);

		String getJustify();

		void setJustify(String justify);

		void setGrade(float grade);

		void show();

		void hide();
	}

	private final HandlerManager eventBus;
	private final CoreServiceAsync rpcService;
	private final EditIndicator2EvaluateDisplay display;
	private Indicator2Evaluate indicatore2Evaluate;

	public EditIndicator2EvaluatePresenter(HandlerManager eventBus,
			CoreServiceAsync rpcService, EditIndicator2EvaluateDisplay display,
			String indicatorId) {

		this.rpcService = rpcService;
		this.eventBus = eventBus;
		this.display = display;
		display.setPresenter(this);

		if (indicatorId == null) {
			indicatore2Evaluate = new Indicator2Evaluate();

		} else {
			loadBean(indicatorId);
		}

	}

	private void loadBean(String featureId) {
		AppController.setStatusMessage("Loading...");
		rpcService.getIndicator2Evaluate(featureId,
				new AsyncCallback<Indicator2Evaluate>() {
					@Override
					public void onSuccess(Indicator2Evaluate result) {
						if (result == null) {
							onFailure(new Throwable("Database error"));
							return;
						}
						indicatore2Evaluate = result;
						display.setName(indicatore2Evaluate.getName());
						display.setDescription(indicatore2Evaluate
								.getDescription());
						display.setWeighing(indicatore2Evaluate.getWeighing());
						display.setJustify(indicatore2Evaluate.getJustify());
						display.setGrade(indicatore2Evaluate.getGrade());
						hideStatusMessage();
					}

					@Override
					public void onFailure(Throwable caught) {
						GWT.log(Constants.MESSAGE_FAILED_GET
								+ Indicator2Evaluate.class.getName(), caught);
						indicatore2Evaluate = new Indicator2Evaluate();
						// TODO i18n
						String failedMessage = "Failed to retrieve indicator2Evaluate";
						display.showInfo(failedMessage);
						hideStatusMessage();
					}

					void hideStatusMessage() {
						AppController.hideStatusMessage();
					}
				});
	}

	@Override
	public void doSaveOrUpdate() {
		indicatore2Evaluate.setName(display.getName());
		indicatore2Evaluate.setDescription(display.getDescription());
		indicatore2Evaluate.setWeighing(display.getWeighing());
		indicatore2Evaluate.setJustify(display.getJustify());
		String currentFeature2EvaluateId = (String) AppController
				.getAttribute(Constants.CURRENT_FEATURE2EVALUATE_ID);
		indicatore2Evaluate.setFeature2Evaluate(new Feature2Evaluate(
				currentFeature2EvaluateId));

		// TODO i18n
		final String failedMessage = "Failed to save indicator2Evaluate";
		final String successMessage = "Indicator2Evaluate saved sucessfully";

		display.hide();
		AppController.setStatusMessage("Saving...");
		rpcService.saveOrUpdateIndicator2Evaluate(indicatore2Evaluate,
				new AsyncCallback<Boolean>() {
					@Override
					public void onSuccess(Boolean result) {
						if (result) {
							eventBus.fireEvent(new ListIndicators2EvaluateEvent());
							display.showInfo(successMessage);
							hideStatusMessage();

						} else {
							onFailure(new Throwable("Database error"));
						}
					}

					@Override
					public void onFailure(Throwable caught) {
						GWT.log(Constants.MESSAGE_FAILED_SAVE
								+ Indicator2Evaluate.class.getName(), caught);
						display.showInfo(failedMessage);
						hideStatusMessage();
					}

					void hideStatusMessage() {
						AppController.hideStatusMessage();
					}
				});
	}

	@Override
	public void go(HasWidgets container) {
		display.show();
	}

}
